package cn.wolfcode.web.controller;

import cn.wolfcode.domain.ConsumptionReportVO;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {

    @Autowired
    private IConsumptionReportService consumptionReportService;

    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo){

        model.addAttribute("businesses",businessService.listAll());

        PageInfo<ConsumptionReportVO> pageInfo = consumptionReportService.list(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "consumptionReport/list";
    }
    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo){
        qo.setPageSize(0);//去除分页查询
        //list 用于存对应的门店 金额 总 优 实 单数
        List<String> groupTypes = new ArrayList<>();
        List<BigDecimal> totalAmounts = new ArrayList<>();
        List<BigDecimal> totalDiscountAmounts = new ArrayList<>();
        List<BigDecimal> totalPayAmounts = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();

        List<ConsumptionReportVO> data = consumptionReportService.list(qo).getList();
        for (ConsumptionReportVO vo : data) {
            groupTypes.add(vo.getGroupType());
            totalAmounts.add(vo.getTotalAmount());
            totalDiscountAmounts.add(vo.getTotalDiscountAmount());
            totalPayAmounts.add(vo.getTotalPayAmount());
            counts.add(vo.getTotalCount());
        }
        //页面所需要的数据
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));
        model.addAttribute("totalDiscountAmounts", JSON.toJSONString(totalDiscountAmounts));
        model.addAttribute("totalPayAmounts", JSON.toJSONString(totalPayAmounts));
        model.addAttribute("counts", JSON.toJSONString(counts));

        return "consumptionReport/listBar";
    }
}
